package java_learn.java_jinjie.ThreadSafe;

/**
 * @Author: 李浩然
 * @Date: 2020/12/21 12:12 上午
 */
public class BAoziPu extends Thread {
    private Baozi bz;

    public BAoziPu(Baozi bz) {
        this.bz = bz;
    }

    @Override
    public void run() {
        int cnt = 0;
        while (true) {
            synchronized (bz) {
                //对包子状态进行判断
                if (bz.flag == true) {
                    try {
                        bz.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //被唤醒之后执行
                if (cnt % 2 == 0) {
                    //生产三鲜
                    bz.pi = "薄皮";
                    bz.xian = "三鲜";
                } else {
                    //生产韭菜
                    bz.pi = "冰皮";
                    bz.xian = "牛肉";
                }
                cnt++;
                System.out.println("包子铺正在生产:" + bz.pi + bz.xian + "包子");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                bz.flag = true;
                bz.notify();
                System.out.println("生产好了" + bz.pi + bz.xian + "包子,可以吃了");
            }
        }
    }
}
